<?php


#sleep(rand(1,10));

echo "<pre>";
/**
 * 
 * 根据用户的粉丝列表抓取可能的粉丝
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}




define("CRON_NAME","cron_baidu_blog_tuiguang");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/gonglue/inc.php");

ini_set("display_errors",ture);
error_reporting(E_ALL);


class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
	
		
		$this->after_run();
	}
	function get_data()
	{
		$this->start_id = $this->start_id + 5;
		$db_gonglue = new db_gonglue();
		$get_list = $db_gonglue->cron_get_gonglue_topic_list(1,100,$this->start_id);
		
		
		foreach($get_list as $key => $value)
		{
			
			
			$content = $value["content"];
			$content_short = preg_replace("/\<.*?\>/","",$content);
			
			var_dump(strlen($content_short));
			
			if(strlen($content_short) > 400)
			{
				$res = $this->do_add_mblog($value["title"],$content_short,$value["id"],$value["topic_id"]);
				
				if($value["id"])$this->replace_into_data($value["id"]);	
				break;
			}
		}
	}	
	
	
	function do_add_mblog($title,$content,$id,$topic_id)
	{
		
		$content = preg_replace("/\<a.*?\>/","",$content);var_dump($content);
		$content = preg_replace("/\<\/a.*?\>/","",$content);var_dump($content);
		$content  = str_replace("/data/urlget/","http://www.yxxiu.com//data/urlget/",$content);
		
		
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$topic_info = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_item_by_id($topic_id);
		$topic = $topic_info["topic"] ? $topic_info["topic"] : "游戏";
		$content = "本文来自-> <a href=\"http://www.yxxiu.com/gonglue/detail.php?id={$id}\" target=\"_blank\">{$title}</a><br><br> ".$content;
		
		$title = "[转载]{$topic}资讯攻略-".$title;
		
		$param = array();
		$param["bdstoken"] = "66d1e528bed1dc4c74033abcd9c8342e";
		$param["content"] = $content;
		$param["imgnum"] = 0;
		$param["qing_request_source"] = "new_request";
		$param["refer"] = "http://hi.baidu.com/home";
		$param["title"] = $title;
		$param["tags[]"][] = "游戏";
		$param["tags[]"][] = $topic;
	
		$url="http://hi.baidu.com/pub/submit/createtext";
		$cookie="BAIDUID=15A28034635C9E8DFAB687CBAF8F18DD:FG=1; Hm_lvt_8c869b543955d43e496c2efee5b55823=1358328292,1359004875,1359095340,1359106903; BDUT=mutz15A28034635C9E8DFAB687CBAF8F18DD13b985aa9921; bdshare_firstime=1355395455365; PSPTOKEN=08c62eb87e167427e2a2248ea35679e903806c1122179627af93af10d340a88e690066981b0a; Hm_lpvt_8c869b543955d43e496c2efee5b55823=1359108879; CANAL=101; BDUSS=lPVU12NEpyajZpZ1RNTkF3RGROenF2VjJ1aEpscXBzNW1EWHk1d3Qyek41aWxSQVFBQUFBJCQAAAAAAAAAAAEAAADujnIJeWFuZ2NoYW9oaQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM1ZAlHNWQJRUG";
		$res = $this->mblog_curl_request("post",$url,$cookie,$param);
		
		
		$res = json_decode($res,true);
		
		var_dump($res);
		
		if($res["errorNo"]=="0" )
		{
			return true;
		}
		else
		{
			return false;
		}
		exit;
		
		
	}
	function mblog_curl_request($type="get",$url,$cookie_list="",$params_arr=array(),$auth_user="",$auth_passwd="",$muliti=false)
	{
		
		if($type == "get")
		{
			$ch = curl_init();
			
			if (strlen($auth_user) > 0)
	        {
	        	curl_setopt($ch, CURLOPT_USERPWD, "{$auth_user}:{$auth_passwd}");  
	        }   
			
	        
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HEADER,true);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			if($cookie_list)
			{
				curl_setopt($ch, CURLOPT_COOKIE, $cookie_list);
			}
	
		}
		elseif($type == "post")
		{
			
			
			
			$fields_string = "";
			if($muliti)
			{
				$fields_string = $this->build_http_query_multi($params_arr);
				$header_array2 = array("Content-Type: multipart/form-data; boundary=" . $this->boundary );
	        }
			else 
			{
				if (is_array($params_arr) && $params_arr)
		        {
			        foreach($params_arr as $key=>$value) 
			        { 
			        	if(is_array($value))
			        	{
			        		foreach($value as $value1)
			        		{
			        			 $fields_string .= $key.'='.urlencode($value1).'&' ; 
			        		}
			        		
			        	}
			        	else
			        	{
			        		 $fields_string .= $key.'='.urlencode($value).'&' ; 
			        	}
			           
			        }
			        $fields_string = rtrim($fields_string ,'&');
		        }
		        else 
		        {
		        	 $fields_string .= $params_arr; //urlencode($params_arr);
		        }
				
				}
           
			
			$ch = curl_init();
			
			if (strlen($auth_user) > 0)
	        {
	        	curl_setopt($ch, CURLOPT_USERPWD, "{$auth_user}:{$auth_passwd}");  
	        }   
	        
	        if($muliti)
	        {
	        	curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array2 ); 
	        }
			curl_setopt($ch, CURLOPT_URL, $url);
			
			
			
			curl_setopt($ch, CURLOPT_REFERER,"http://hi.baidu.com/pub/show/createtext");
			curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0");
			
			
			curl_setopt($ch, CURLOPT_HEADER,true);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_POST,count($params_arr));
			curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
			
			if($cookie_list)
			{
				curl_setopt($ch, CURLOPT_COOKIE, $cookie_list);
			}
			 
		}
		
		//$time_start = microtime_float();
		$res = curl_exec($ch);
		
		$res2 = curl_getinfo($ch);
		curl_close($ch);		
		//$time_using = microtime_float() - $time_start;
		//global $program_wapi_call_time;
		//$program_wapi_call_time = $program_wapi_call_time + $time_using;
		// 性能监控
		//$this->perf_add_count($url);                              //加入性能分析
	
		
		$res = explode("\r\n\r\n",$res);
		$content_json = array_pop($res);
		$header = array_pop($res);
		
		$content = json_decode($content_json, TRUE);
		
		
		
		$params_arr["cookie_list"] = $cookie_list;
		$params_arr["basic_info"] = $auth_user;
		
		/*if(substr($header,0,15) != "HTTP/1.1 200 OK")
		{
			$error_code = substr($header,9,3);
			$error_code = intval($error_code) * 1000;
			$this->add_api_tongji_v2($url,$error_code,$content['error'],$header,serialize($params_arr),$time_using,$content_json);
		}
		else
		{
			//$this->add_api_tongji_v2($url,$content['error_code'],$content['error'],$header,serialize($params_arr),$time_using,$content_json);
		}*/
		
		
		if(isset($_GET["mblog_debug"]))
		{
			echo "<pre>";
			echo "request_url:".$url."\n";
			echo "type: $type<br>\n\r";
			echo "auth_user: $auth_user<br>\n\r";
			echo "auth_passwd: $auth_passwd<br>\n\r";
			echo "cookie_list: $cookie_list<br>\n\r";
			echo "request_result:";var_dump($res);
			echo "params:";
			var_dump($params_arr);
			echo "fields_string:";
			var_dump($fields_string);
		}
		
		return $content_json;

	}
	
	
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
